package butines.core.game.util;

import java.awt.Graphics2D;
import java.text.MessageFormat;

import butines.core.math.Vector2f;

public class GUtils {

	public static int LEFT   = -1;
	public static int CENTER = 0;
	public static int RIGHT  = 1;
	
	public static int TOP    = 1;
	public static int MIDDLE = 0;
	public static int BOTTOM = -1;
	
	public static float halign(float x, float w, int halign) {
		
		if (halign > 0) {
			return x - w;
		}
		if (halign == 0) {
			return x - w / 2;
		}
		return x;
	}

	public static float valign(float y, float h, int valign) {
		if (valign > 0) {
			return y + h;
		}
		if (valign == 0) {
			return y + h / 2;
		}
		return y;
	}
	
	public static int getFontWidth(Graphics2D g, String str) {
		return g.getFontMetrics().stringWidth(str);
	}
	
	public static int getFontHeight(Graphics2D g) {
		return g.getFontMetrics().getHeight();
	}

	public static void print(Graphics2D g, float x, float y, int halign, int valign, String pattern, Object ... args) {
		String str = MessageFormat.format(pattern, args);
		int w = g.getFontMetrics().stringWidth(str);
		int h = g.getFontMetrics().getHeight();
		g.drawString(str, halign(x, w, halign), valign(y, h, valign));
	}
	
	public static void drawOval(Graphics2D g, float x, float y, float w, float h) {
		g.drawOval(Math.round(x), Math.round(y), Math.round(w), Math.round(h));
	}

	public static void drawOval(Graphics2D g, float x, float y, float radius) {
		drawOval(g, x - radius, y - radius, radius * 2, radius * 2);
	}
	
	public static void drawOval(Graphics2D g, Vector2f v, float radius) {
		drawOval(g, v.x - radius, v.y - radius, radius * 2, radius * 2);
	}
	
	public static void drawLine(Graphics2D g, float x1, float y1, float x2, float y2) {
		g.drawLine(Math.round(x1), Math.round(y1), Math.round(x2), Math.round(y2));
	}
	
	public void drawLine(Graphics2D g, Vector2f p1, Vector2f p2) {
		drawLine(g, p1.x, p1.y, p2.x, p2.y);
	}

}
